package com.joint.web.action.main;

import com.fz.us.base.dao.jdbc.JdbcDao;
import com.fz.us.base.util.DataUtil;
import com.joint.base.service.AdminService;
import com.joint.base.service.CompanyService;
import com.joint.base.service.UsersService;
import com.joint.web.action.BaseAdminAction;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.hibernate.SessionFactory;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by amin on 2015/5/5.
 */
@ParentPackage("main")
public class AjaxAction extends BaseAdminAction {
    @Resource
    protected JdbcDao jdbcDao;
    @Resource
    protected AdminService adminService;
    @Resource
    protected UsersService usersService;
    @Resource
    protected CompanyService companyService;
    @Resource
    protected SessionFactory sessionFactory;

    private long companyNumber;
    private long usersNumber;
    private long companyNumberToday;
    private long usersNumberToday;

    public String dashboard(){
        String users_sql = "SELECT count(*) as perCount from sys_admin ";
        String company_sql = "SELECT count(*) as perCount from sys_company WHERE state = 'Enable' ";
        String usersToday_sql = "SELECT count(*) as perCount from sys_admin where date_format(createDate, '%Y-%m-%d') = :date";
        String companyToday_sql = "SELECT count(*) as perCount from sys_company WHERE state = 'Enable' and date_format(createDate, '%Y-%m-%d') = :date";

        Map<String,String> filter = new HashMap<String,String>();
        filter.put("date", DataUtil.DateToString(new Date()));

        List<Map<String, Object>> tmpListMap;
        tmpListMap = jdbcDao.findForListMap(users_sql,null);
        if(null != tmpListMap && tmpListMap.size()>0) {
            Map lMap = tmpListMap.get(0);
            usersNumber = (long)lMap.get("perCount");
        }
        tmpListMap = jdbcDao.findForListMap(company_sql,null);
        if(null != tmpListMap && tmpListMap.size()>0) {
            Map lMap = tmpListMap.get(0);
            companyNumber = (long)lMap.get("perCount");
        }
        tmpListMap = jdbcDao.findForListMap(usersToday_sql,filter);
        if(null != tmpListMap && tmpListMap.size()>0) {
            Map lMap = tmpListMap.get(0);
            usersNumberToday = (long)lMap.get("perCount");
        }
        tmpListMap = jdbcDao.findForListMap(companyToday_sql,filter);
        if(null != tmpListMap && tmpListMap.size()>0) {
            Map lMap = tmpListMap.get(0);
            companyNumberToday = (long)lMap.get("perCount");
        }
        return "dashboard";
    }
    public String users(){
        return "users";
    }
    public String company(){
        return "company";
    }
    public String companyData(){
        return "companyData";
    }
    public String dict(){
        return "dict";
    }

    public String authcode(){
        return "authcode";
    }
    public long getCompanyNumber() {
        return companyNumber;
    }

    public void setCompanyNumber(long companyNumber) {
        this.companyNumber = companyNumber;
    }

    public long getUsersNumber() {
        return usersNumber;
    }

    public void setUsersNumber(long usersNumber) {
        this.usersNumber = usersNumber;
    }

    public long getCompanyNumberToday() {
        return companyNumberToday;
    }

    public void setCompanyNumberToday(long companyNumberToday) {
        this.companyNumberToday = companyNumberToday;
    }

    public long getUsersNumberToday() {
        return usersNumberToday;
    }

    public void setUsersNumberToday(long usersNumberToday) {
        this.usersNumberToday = usersNumberToday;
    }
}
